package com.sdtv.mediunfusion.mvp.presenter;

import com.sdtv.basemodule.mvp.presenter.BasePresenter;
import com.sdtv.basemodule.rx.BaseObserver;
import com.sdtv.mediunfusion.bean.SelectTopicBean;
import com.sdtv.mediunfusion.mvp.contract.MainContract;
import com.sdtv.mediunfusion.mvp.model.SelectTopicListModel;

import java.util.List;

/**
 * @ProjectName: MediunFusion
 * @Package: com.sdtv.mediunfusion.mvp.presenter
 * @ClassName: SelectTopicListPresenter
 * @Description: java类作用描述
 * @Author: 小郭同学
 * @CreateDate: 2019-08-05 14:42
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class SelectTopicListPresenter extends BasePresenter<MainContract.SelectTopicListView,
        SelectTopicListModel> {
    // 构造model
    @Override
    public void initRepository() {
        mModel = new SelectTopicListModel(mView);
    }

    /**
     * 加载列表数据
     *
     * @param page
     */
    public void getSelectListData(int page) {
        mModel.getListData(page).subscribe(new BaseObserver<SelectTopicBean>(SelectTopicBean.class) {
            /**
             * 获取到列表数据 且不为空
             * @param result
             */
            @Override
            public void doNextList(int totalCount, List<SelectTopicBean> result) {
                // 有列表数据
                if (null != result && !result.isEmpty()) {
                    mView.setListData(totalCount, result);
                } else {
                    // 没有列表数据时  执行onEmpty
                    mView.showEmptyView();
                }
            }

            // 获取列表数据为空
            @Override
            public void onEmpty() {
                mView.showEmptyView();
            }

            // 加载数据失败了
            @Override
            public void onError(Throwable e) {
                mView.onError();
            }
        });
    }

    /**
     * 获取选题详情信息
     *
     * @param topicId
     */
    public void getTopicInfo(String topicId) {
        mModel.getTopicDetail(topicId).subscribe(new BaseObserver<SelectTopicBean>(SelectTopicBean.class) {
            // 获取详情信息
            @Override
            public void doNext(SelectTopicBean selectTopicBean) {
                mView.setTopicInfo(selectTopicBean);
            }

            // 未获取到数据
            @Override
            public void onEmpty() {
                mView.onError();
            }

            // 接口请求失败了
            @Override
            public void onError(Throwable e) {
                mView.onError();
            }
        });
    }
}
